﻿#region 命名空间

using System;
using System.Text;
using System.Text.RegularExpressions;

#endregion

namespace Sharp.Infrastructure
{
    /// <summary>
    ///     string类扩展
    /// </summary>
    public static class MyString
    {
        #region 返回指定字符串的单字节长度

        /// <summary>
        ///     返回指定字符串的单字节长度
        /// </summary>
        /// <param name="strInPut"></param>
        /// <returns></returns>
        public static int Length(string strInPut)
        {
            Encoding encoding = Encoding.Default;
            byte[] strbytes = encoding.GetBytes(strInPut);
            return strbytes.Length;
        }

        #endregion

        #region  截短字串的函数，兼容中英文

        /// <summary>
        ///     截短字串的函数，兼容中英文
        /// </summary>
        /// <param name="mText">要加工的字串</param>
        /// <param name="byteCount">长度</param>
        /// <returns>被加工过的字串</returns>
        public static string Left(string mText, int byteCount)
        {
            if (string.IsNullOrEmpty(mText))
            {
                return mText;
            }
            if (byteCount < 1)
                return mText;

            if (Encoding.Default.GetByteCount(mText) <= byteCount)
            {
                return mText;
            }
            else
            {
                byte[] txtBytes = Encoding.Default.GetBytes(mText);
                byte[] newBytes = new byte[byteCount - 4];

                for (int i = 0; i < byteCount - 4; i++)
                {
                    newBytes[i] = txtBytes[i];
                }
                string outPut = Encoding.Default.GetString(newBytes) + "...";
                if (outPut.EndsWith("?...") == true)
                {
                    outPut = outPut.Substring(0, outPut.Length - 4);
                    outPut += "...";
                }
                return outPut;
            }
        }

        #endregion

        #region  截短字串的函数，不分区中英文

        /// <summary>
        ///     截短字串的函数，不分区中英文
        /// </summary>
        /// <param name="inPut"></param>
        /// <param name="Long"></param>
        /// <returns></returns>
        public static string Left(object inPut, int Long)
        {
            if (Long >= inPut.ToString().Length)
            {
                return inPut.ToString();
            }
            else
            {
                string outPut = inPut.ToString().Substring(0, Long - 3) + "...";
                if (outPut.EndsWith("?..."))
                {
                    outPut = outPut.Substring(0, outPut.Length - 4);
                    outPut += "...";
                }
                return outPut;
            }
        }

        #endregion

        #region 去除非法字串

        /// <summary>
        ///     去除非法字串
        /// </summary>
        /// <param name="inPut">原字串</param>
        /// <returns>过滤过的字串</returns>
        public static string ReplaceBadChar(string inPut)
        {
            if (inPut.Trim().Length == 0)
            {
                return string.Empty;
            }
            else
            {
                StringBuilder outPut = new StringBuilder();
                outPut.Append(inPut);
                outPut.Replace("'", "");
                outPut.Replace("*", "");
                outPut.Replace("?", "");
                outPut.Replace("(", "");
                outPut.Replace(")", "");
                outPut.Replace("<", "");
                outPut.Replace("=", "");

                return outPut.ToString().Trim();
            }
        }

        #endregion

        #region 转义SQL中的限制符号

        /// <summary>
        ///     转义SQL中的限制符号
        /// </summary>
        /// <param name="inPut"></param>
        /// <returns></returns>
        public static string SQL_ESC(string inPut)
        {
            StringBuilder outPut = new StringBuilder();
            outPut.Append(inPut);
            outPut.Replace("[", "[[]");
            outPut.Replace("%", "[%]");
            outPut.Replace("_", "[_]");
            outPut.Replace("'", "''");

            return outPut.ToString().Trim();
        }

        #endregion

        #region 替换中文数字输入字符

        /// <summary>
        ///     替换中文数字输入字符
        /// </summary>
        /// <param name="input">输入字符</param>
        /// <returns></returns>
        public static string ReplaceString(string input)
        {
            StringBuilder output = new StringBuilder(input);
            output.Replace("０", "0");
            output.Replace("１", "1");
            output.Replace("２", "2");
            output.Replace("３", "3");
            output.Replace("４", "4");
            output.Replace("５", "5");
            output.Replace("６", "6");
            output.Replace("７", "7");
            output.Replace("８", "8");
            output.Replace("９", "9");
            output.Replace("．", ".");

            return output.ToString();
        }

        #endregion

        #region 删除多余的空行

        /// <summary>
        ///     删除多余的空行
        /// </summary>
        /// <param name="inPut"></param>
        /// <returns></returns>
        public static string DeleteBlankLine(string inPut)
        {
            if (inPut.Length > 0)
            {
                return Regex.Replace(inPut, @"(\n[\s| ]*\r)", "");
            }
            else
            {
                return inPut;
            }
        }

        #endregion

        #region 格式化ＸＭＬ禁用符号

        /// <summary>
        ///     格式化ＸＭＬ禁用符号
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string FormatForXml(string input)
        {
            StringBuilder outPut = new StringBuilder(input);
            outPut.Replace("<", "&lt;");
            outPut.Replace(">", "&gt");
            outPut.Replace("&", "&amp;");
            outPut.Replace("\"", "&quot;");
            outPut.Replace("'", "&apos;");
            outPut.Replace(" ", "#x20");

            return outPut.ToString();
        }

        #endregion

        #region 转半角(DBC case)

        /// <summary>
        ///     转半角(DBC case)
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>半角字符串</returns>
        public static string ToDbc(this string input)
        {
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288)
                {
                    c[i] = (char) 32;
                    continue;
                }
                if (c[i] > 65280 && c[i] < 65375)
                    c[i] = (char) (c[i] - 65248);
            }
            return new string(c);
        }

        #endregion

        #region 取得开始、结束字符之间的字符串极其本身

        /// <summary>
        ///     取得开始、结束字符之间的字符串极其本身
        /// </summary>
        /// <param name="input"></param>
        /// <param name="startTag"></param>
        /// <param name="endTag"></param>
        /// <returns></returns>
        public static string GetString(string input, string startTag, string endTag)
        {
            int s = input.IndexOf(startTag, StringComparison.CurrentCultureIgnoreCase);
            if (s < 0)
                throw new ArgumentOutOfRangeException("开始标记不存在");


            int e = input.IndexOf(endTag, s + startTag.Length, StringComparison.CurrentCultureIgnoreCase);


            return input.Substring(s, e + endTag.Length - s).Trim();
        }

        #endregion

        #region 取得开始、结束字符之间的字符串

        /// <summary>
        ///     取得开始、结束字符之间的字符串
        /// </summary>
        /// <param name="input"></param>
        /// <param name="startTag"></param>
        /// <param name="endTag"></param>
        /// <returns></returns>
        public static string GetInnerString(string input, string startTag, string endTag)
        {
            string str = "";
            int index = input.IndexOf(startTag, 0, StringComparison.CurrentCultureIgnoreCase);
            if (index >= 0)
            {
                int i = input.IndexOf(endTag, index, StringComparison.CurrentCultureIgnoreCase);
                if (i >= 0)
                {
                    str = input.Substring(index + startTag.Length, (i - index) - startTag.Length);
                }
            }
            return str;
        }

        #endregion
    }
}